# vim: syntax=bash
@options timing

@define NEXT_VERSION  '0.2.1'
@define TUSH_REPO     'https://github.com/adolfopa/tush'
@define GOAWK_VERSION '1.29.1' # https://github.com/benhoyt/goawk/releases
@define GOAWK         "./soft/goawk$GOAWK_VERSION"

@goal soft_folder_created @private
@reached_if [[ -d "soft" ]]
  mkdir soft

@lib dl
  download() {
    local url="$1"
    local file="$2"

    if command -v wget >/dev/null
    then
      wget "$url" -O"$file"
    elif command -v curl >/dev/null
    then
      curl -L "$url" -o "$file"
    else
      false
    fi
  }

@goal tush_installed @private
@depends_on soft_folder_created
@reached_if [[ -f "soft/tush/bin/tush-check" ]]
@use_lib dl
  echo
  echo "Fetching tush..."
  echo

  cd "soft"

  if download "$TUSH_REPO/archive/master.tar.gz" ./tush.tar.gz
  then
    tar xzvf ./tush.tar.gz
    rm ./tush.tar.gz
    mv tush-master tush
  else
    git clone --depth 1 $TUSH_REPO.git
    rm -r tush/.git
  fi

@goal default
@doc 'same as tested_by_default_awk'
@depends_on tested_by_default_awk

@goal testall
@doc 'runs test suite'
@depends_on tested_by_default_awk
@depends_on tested_by_bwk
@depends_on tested_by_gawk
@depends_on tested_by @args 'tush'   'mawk -f ./fhtagn.awk'
@depends_on tested_by @args 'fhtagn' 'mawk -f ./fhtagn.awk'
@depends_on tested_by_busybox_awk
@depends_on tested_by_goawk

@goal tested_by_bwk
@depends_on installed_bwk
@depends_on tested_by @args 'tush'   './soft/bwk -f ./fhtagn.awk'
@depends_on tested_by @args 'fhtagn' './soft/bwk -f ./fhtagn.awk'

@goal tested_by_gawk
@depends_on tested_by @args 'tush'   'gawk -f ./fhtagn.awk'
@depends_on tested_by @args 'fhtagn' 'gawk -f ./fhtagn.awk'

@goal tested_by_default_awk
@depends_on tested_by @args 'tush'   './fhtagn.awk'
@depends_on tested_by @args 'fhtagn' './fhtagn.awk'

@goal tested_by_busybox_awk
@depends_on installed_busybox
@depends_on tested_by @args 'tush'   './soft/busybox awk -f ./fhtagn.awk'
@depends_on tested_by @args 'fhtagn' './soft/busybox awk -f ./fhtagn.awk'

@goal tested_by_goawk
@depends_on installed_goawk
@depends_on tested_by @args 'tush'   "$GOAWK -f ./fhtagn.awk"
@depends_on tested_by @args 'fhtagn' "$GOAWK -f ./fhtagn.awk"

@goal tested_by @params TOOL FHTAGN
@depends_on tush_installed
  export MYDIR="$(pwd)"
  f=tests/fhtagn.tush

  echo "Testing with $TOOL ($FHTAGN)..."

  calc_temp_files() {
    local tmp_count=$(find /tmp -maxdepth 1 -type f -name 'fhtagn.*' | wc -l)
    local cnt
    (( cnt = tmp_count ))
    if [[ -d "/dev/shm" ]]
    then
      local shm_count=$(find /dev/shm -maxdepth 1 -type f -name 'fhtagn.*' | wc -l)
      (( cnt += shm_count ))
    fi
    echo $cnt
  }

  test_file() {
    if [[ $TOOL == "tush" ]]
    then
      export PATH="$PATH:$MYDIR/soft/tush/bin"
      DIFF="diff --strip-trailing-cr" tush-check "$f"
    else
      local before_count=$(calc_temp_files)
      ./fhtagn.awk "$f"
      local after_count=$(calc_temp_files)
      if (( before_count != after_count ))
      then
        echo >&2 "!!! temp file not deleted !!!"
        exit 1
      fi
    fi
  }

  if test_file
  then
    echo "TESTS PASSED : $f"
  else
    echo >&2 "!!! TESTS FAILED !!! : $f"
    exit 1
  fi

@goal installed_bwk @private
@reached_if [[ -f soft/bwk ]]
@depends_on soft_folder_created
@use_lib dl
  echo
  echo "Fetching BWK..."
  echo

  cd "soft"

  download 'https://github.com/onetrueawk/awk/archive/refs/heads/master.tar.gz' bwk.tar.gz
  tar xzvf bwk.tar.gz
  rm bwk.tar.gz

  echo
  echo "Compile BWK..."
  echo

  cd "awk-master"

  make

  mv a.out ../bwk

  cd ..
  ./bwk --version
  rm -r awk-master

@goal installed_busybox @private
@reached_if [[ -x ./soft/busybox ]]
@use_lib dl
  download 'https://busybox.net/downloads/binaries/1.31.0-i686-uclibc/busybox' ./soft/busybox
  chmod +x ./soft/busybox
  echo "Installed: $(./soft/busybox | head -n 1)"

@goal installed_goawk @private
@reached_if [[ -f $GOAWK ]]
@depends_on soft_folder_created
@use_lib dl
  echo
  echo "Fetching GoAWK $GOAWK_VERSION ..."
  echo

  cd "soft"

  if [[ "$OSTYPE" == "linux-gnu"* ]]; then
    os="linux"
  elif [[ "$OSTYPE" == "darwin"* ]]; then
    os="darwin"
  else
    >&2 echo "Unknown OS"
    exit 1
  fi

  F=goawk_v${GOAWK_VERSION}_${os}_amd64.tar.gz
  download "https://github.com/benhoyt/goawk/releases/download/v$GOAWK_VERSION/$F" "$F"
  tar xzvf "$F" goawk
  rm "$F"

  mv goawk "goawk$GOAWK_VERSION"
  "./goawk$GOAWK_VERSION" --version

@goal update_readme
@doc 'updates the README.md with the current output of the tool'
  awk '
  !Off
  Examples {
    if (/^\$/) { Off=1; system(substr($0,2)) }
    else if (/^```$/ && Off) { Off=0; print }
  }
  /## Usage/ { Examples=1 }
  ' README.md > README.md.1
  mv README.md.1 README.md


@define COVERPROFILE "/tmp/cov_fhtagn.txt"

@goal _cover_profile_deleted @private
  rm -f "$COVERPROFILE"

@goal _cover_profile_prepared @private
@depends_on _cover_profile_deleted
@depends_on tested_by @args 'fhtagn' "$GOAWK -covermode=set -coverprofile=$COVERPROFILE -coverappend -f ./fhtagn.awk"

@goal coverage
@doc 'prepares code coverage report'
@depends_on _cover_profile_prepared
  go tool cover -html="$COVERPROFILE"

@goal coverage_badge_updated
@doc 'updates code coverage badge'
@depends_on _cover_profile_prepared
  html='/tmp/cov.html'
  go tool cover -html="$COVERPROFILE" -o "$html"
  coverage="$(awk '/fhtagn\.awk/ { if (match($0,/\(.+\)/)) print substr($0,RSTART+1,RLENGTH-2) }' "$html")"
  echo "coverage: $coverage"
  awk -v coverage="$coverage" '{ gsub(/\$COVERAGE/,coverage) } 1' coverage.tpl.svg > coverage.svg
  rm "$html"

@goal release_prepated
@doc 'prepares coverage badge & README'
@depends_on coverage_badge_updated
@depends_on update_readme

@goal gh_release
@doc 'publish Github release (draft)'

  fail () {
    echo >&2 "$@"
    exit 1
  }

  releaseTag="v$NEXT_VERSION"
  echo
  echo "Will publish a draft release for $releaseTag ..."
  echo

  notesStr="[Issues included](https://github.com/xonixx/fhtagn/issues?q=is%3Aclosed+milestone%3A${NEXT_VERSION})"

  echo "Notes: $notesStr"

  gh release create "$releaseTag" \
    --title "$releaseTag" \
    --notes "$notesStr" \
    --draft

  echo "Please don't forget to open the release draft, edit it as needed and PUBLISH!"

